package com.example.springbootdemo.api.controller;

import com.example.springbootdemo.domin.VO.getCourseDetailVO;
import com.example.springbootdemo.domin.entity.Course;
import com.example.springbootdemo.domin.entity.Teacher;
import com.example.springbootdemo.infra.util.Result;
import com.example.springbootdemo.app.service.TeacherService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Api(value = "教师相关controller", tags = {"教师相关接口"})
@RequestMapping("teacher")
@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    private final Logger logger = LoggerFactory.getLogger(TeacherController.class);

    /**
     * 查询老师信息
     *
     * @param tno
     * @return
     */
    @GetMapping("/getTeacher/{tno}")
    public ModelAndView getTeacher(@PathVariable("tno") Integer tno) {
        if (tno == null) {
            //throw new Exception();todo
        }
        logger.info("要查询的老师工号为:{}", tno);
        Teacher teacher = teacherService.getTeacher(tno);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("change_teacher");
        modelAndView.addObject("teacher", teacher);
        return modelAndView;
    }

    /**
     * 更新老师信息
     *
     * @param teacher
     * @return
     */
    @PostMapping("/updateTeacher")
    public ModelAndView updateTeacher(Teacher teacher) {
        teacherService.updateTeacher(teacher);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("teacher");
        List<Teacher> all = teacherService.getAll();
        modelAndView.addObject("tc", all);
        return modelAndView;
    }


    /**
     * 删除老师
     *
     * @param tno
     * @return
     */
    @GetMapping("/deleteTeacher/{tno}")
    public ModelAndView deleteTeacher(@PathVariable("tno") Integer tno) {
        if (tno == null) {
            //throw new Exception();todo
        }
        teacherService.deleteTeacher(tno);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("teacher");
        List<Teacher> all = teacherService.getAll();
        modelAndView.addObject("tc", all);
        return modelAndView;
    }

    /**
     * 添加老师
     *
     * @param teacher
     * @return
     */
    @PostMapping("/addTeacher")
    public ModelAndView addTeacher(Teacher teacher) {
        teacherService.addTeacher(teacher);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("teacher");
        List<Teacher> all = teacherService.getAll();
        modelAndView.addObject("tc", all);
        return modelAndView;
    }

    /**
     * 添加多个老师
     *
     * @param list
     * @return
     */
    @PostMapping("/addTeachers")
    public Result addTeachers(@RequestBody List<String> list) {
        teacherService.addTeachers(list);
        return Result.success();
    }

    /**
     * 根据老师的姓名查询多个老师
     *
     * @return
     */
    @PostMapping("/getTeachers")
    public Result<List<Teacher>> getTeachers(@RequestBody List<String> list) {
        List<Teacher> teachers = teacherService.getTeachers(list);
        return Result.success(teachers);
    }

    @GetMapping("/getAll")
    public ModelAndView getAll() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("teacher");
        List<Teacher> all = teacherService.getAll();
        modelAndView.addObject("tc", all);
        return modelAndView;
    }

    /**
     * 根据老师姓名来获取相关课程信息
     *
     * @param tname
     * @return
     */
    @GetMapping("/getCnameByTno/{tname}")
    public Result<List<getCourseDetailVO>> getCnameByTno(@PathVariable("tname") String tname) {
        List<getCourseDetailVO> courseByTno = teacherService.getCourseByTno(tname);
        return Result.success(courseByTno);
    }


}
